package com.sheliduan.itlearning.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sheliduan.itlearning.admin.model.vos.tag.AddTagReqVO;
import com.sheliduan.itlearning.admin.model.vos.tag.DeleteTagReqVO;
import com.sheliduan.itlearning.admin.model.vos.tag.FindTagPageListReqVO;
import com.sheliduan.itlearning.admin.model.vos.tag.UpdateTagReqVO;
import com.sheliduan.itlearning.admin.service.AdminTagService;
import com.sheliduan.itlearning.common.domain.dos.CategoryDO;
import com.sheliduan.itlearning.common.domain.dos.TagDO;
import com.sheliduan.itlearning.common.domain.dos.UserDO;
import com.sheliduan.itlearning.common.domain.mapper.TagMapper;
import com.sheliduan.itlearning.common.enums.ErrorCodeEnum;
import com.sheliduan.itlearning.common.exception.BizException;
import com.sheliduan.itlearning.common.utils.PageResponseData;
import com.sheliduan.itlearning.common.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 断舍离
 * @date: 2025/2/18 8:57
 * @description: 后台管理员标签Service实现类
 **/
@Service
public class AdminTagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements AdminTagService {

    @Autowired
    private TagMapper tagMapper;

    @Override
    public PageResponseData findTagPageList(FindTagPageListReqVO reqVO) {
        long current = reqVO.getCurrent();
        long size = reqVO.getSize();
        String name = reqVO.getName();
        LocalDateTime startDate = reqVO.getStartDate();
        LocalDateTime endDate = reqVO.getEndDate();
        Page<TagDO> page = tagMapper.selectPageList(current, size, name, startDate, endDate);
        List<TagDO> tagDOS = page.getRecords();
        return PageResponseData.success(page, tagDOS);
    }

    @Override
    @Transactional
    public ResponseData addTag(AddTagReqVO reqVO) {

        Integer categoryId = reqVO.getCategoryId();
        // 获取前端传来的标签字符串集合
        List<String> tags = reqVO.getTags();

        // 将字符串集合封装为DO集合
        List<TagDO> dos = tags.stream().map(tagName -> TagDO.builder()
                        .name(tagName.trim())
                        .categoryId(categoryId)
                        .createTime(new Date())
                        .updateTime(new Date())
                        .build())
                .collect(Collectors.toList());

        // 由于数据库标签名是唯一的, 若插入已经存在的可能报错, 直接抛出异常即可
        try {
            // 继承ServiceImpl<TagMapper, TagDO>,使用saveBatch方法可实现批量插入
            saveBatch(dos);
        }catch (Exception e){
            throw new BizException(ErrorCodeEnum.TAG_IS_EXIST);
        }
        return ResponseData.success();
    }



    @Override
    public ResponseData deleteTag(DeleteTagReqVO reqVO) {
        TagDO tagDO = tagMapper.selectById(reqVO.getId());
        if(tagDO.getArticleTotal()+tagDO.getVideoTotal() != 0) {
            return ResponseData.fail("该标签下还有课程, 无法删除");
        }
        int i = tagMapper.deleteById(reqVO.getId());
        if(i > 0)
            return ResponseData.success();
        else
            return ResponseData.fail("删除标签失败");
    }

    @Override
    public ResponseData updateTag(UpdateTagReqVO reqVO) {
        int i = tagMapper.updateTag(reqVO.getId(), reqVO.getName());
        if(i > 0)
            return ResponseData.success();
        else
            return ResponseData.fail("修改标签失败");
    }

    @Override
    public ResponseData selectById(Integer id) {
        TagDO tagDO = tagMapper.selectById(id);
        if(tagDO != null)
            return ResponseData.success(tagDO);
        else
            return ResponseData.fail("该标签不存在");
    }
}
